﻿Imports System.Xml
Imports System.IO
Imports System.Globalization
Imports System.Threading

Public Class Login

    Public Sub New()
        ' Set the form language
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    End Sub

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' If previous login datas exist, use them
        Dim decryptedXML, strserver, strmail, strpassword As String
        Try
            ' Try to get user information if some are stored
            If System.IO.File.Exists(Environ("AppData") & "\" & Application.ProductName & "\" & Reader.user_info_file) = True Then
                Dim ObjXml As New XmlDocument()
                ' Get file user info content and decrypt it
                Dim objReader As New StreamReader(Environ("AppData") & "\" & Application.ProductName & "\" & Reader.user_info_file)
                decryptedXML = functions.Decrypt(objReader.ReadToEnd)
                objReader.Close()
                ObjXml.LoadXml(decryptedXML)
                strserver = ObjXml.DocumentElement.SelectSingleNode("/user/server").InnerText
                strmail = ObjXml.DocumentElement.SelectSingleNode("/user/email").InnerText
                strpassword = ObjXml.DocumentElement.SelectSingleNode("/user/password").InnerText
                server.Text = strserver
                mail.Text = strmail
                password.Text = strpassword
                ObjXml = Nothing
                If server.Text <> "" Then       ' If no server saved, then there are no user info stored so don't try to connect
                    Dim response As String = functions.DoLogin(strserver, strmail, strpassword)
                    'MessageBox.Show(response)
                    If (response <> "KO") And (response <> "403") Then
                        ' OK programm goes on
                        functions.APIKey = response
                        Reader.Show()
                        Exit Sub
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(My.Resources._code_error_occured & " : " & ex.Message)
        End Try
        Me.Show()
        server.Focus()
    End Sub


    Private Sub submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit.Click
        If functions.UrlIsValid(server.Text) = False Then
            MessageBox.Show(My.Resources._code_server_invalid)
            server.Focus()
            Exit Sub
        End If

        If functions.MailIsValid(mail.Text) = False Then
            MessageBox.Show(My.Resources._code_mail_invalid)
            mail.Focus()
            Exit Sub
        End If

        submit.Text = My.Resources._code_connecting
        Dim response As String = functions.DoLogin(server.Text, mail.Text, password.Text)
        Select Case response
            Case "KO"
                submit.Text = My.Resources._code_submit
                MessageBox.Show(My.Resources._code_connexion_ko)
                mail.Focus()
            Case "403"
                submit.Text = My.Resources._code_submit
                MessageBox.Show(My.Resources._code_authentification_ko)
                mail.Focus()
            Case Else
                ' OK programm goes on
                functions.APIKey = response
                Reader.Show()
        End Select

    End Sub

    Private Sub server_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles server.TextChanged
        If server.Text <> "" And mail.Text <> "" And password.Text <> "" Then
            submit.Enabled = True
        Else
            submit.Enabled = False
        End If
    End Sub

    Private Sub mail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mail.TextChanged
        If server.Text <> "" And mail.Text <> "" And password.Text <> "" Then
            submit.Enabled = True
        Else
            submit.Enabled = False
        End If
    End Sub

    Private Sub password_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
        If server.Text <> "" And mail.Text <> "" And password.Text <> "" Then
            submit.Enabled = True
        Else
            submit.Enabled = False
        End If
    End Sub

    Private Sub mail_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles mail.KeyPress
        Dim KeyAscii As Short = Asc(e.KeyChar)
        If KeyAscii = 13 Then
            e.Handled = True
            password.Focus()
        End If
    End Sub

    Private Sub password_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles password.KeyPress
        Dim KeyAscii As Short = Asc(e.KeyChar)
        If KeyAscii = 13 Then
            e.Handled = True
            submit.Enabled = True
            submit_Click(password, System.EventArgs.Empty)
        End If
    End Sub

    Private Sub mail_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mail.Enter
        mail.SelectAll()
    End Sub

    Private Sub password_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.Enter
        password.SelectAll()
    End Sub

End Class